package com.shuogg.transaction.dao.impl;

import com.shuogg.transaction.dao.UserDao;
import com.shuogg.transaction.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public long add(User user) {
        String sql = "INSERT INTO user(name, age) VALUES(?,?) ";
        Object[] args = {user.getName(), user.getAge()};
        PreparedStatementCreator psc = con -> {
            PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            return ps;
        };
        KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(psc, generatedKeyHolder);
        return generatedKeyHolder.getKey().longValue();
    }

    @Override
    public void update(User user) {

    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public List<User> listAll() {
        String sql = "select * from user";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class));
    }

    @Override
    public Long count() {
        return null;
    }

    @Override
    public Long countBy(String field, Object value) {
        return null;
    }

    @Override
    public void deleteBy(String field, Object value) {

    }

    @Override
    public User get(Long id) {
        return null;
    }

    @Override
    public void batchDelete(List<Long> idList) {

    }

    @Override
    public User getBy(String field, Object value) {
        return null;
    }

    @Override
    public User getByAnd(String field1, Object value1, String field2, Object value2) {
        return null;
    }

    @Override
    public User getByOr(String field1, Object value1, String field2, Object value2) {
        return null;
    }

    @Override
    public List<User> listBy(String field, Object value) {
        String sql = "select * from user where " + field + " =? ";
        Object[] args = {value};
        return jdbcTemplate.query(sql, args, new BeanPropertyRowMapper(User.class));
    }

    @Override
    public List<User> listByAnd(String field1, Object value1, String field2, Object value2) {
        return null;
    }

    @Override
    public List<User> listByOr(String field1, Object value1, String field2, Object value2) {
        return null;
    }
}
